﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StudentManage.Data;
using System.Text;
using StudentManage.Common;

namespace StudentManage.Web.Admin.SelectInfo
{
    public partial class SelectDeptDialog : SecurityPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.HidIsRadio.Value = Request.QueryString["IsRadio"].ToString();
                this.HidSelectID.Value = Request.QueryString["ID"].ToString();

                if (Request.QueryString["IsSelectBJ"].ToString() == "0") //判断要选择的是否为班级
                {
                    this.TVDataBind();
                }
                else
                {
                    this.TV_LoadBJ();
                }

                if (Request.QueryString["DeptID"] != null && Request.QueryString["DeptID"].ToString() != "") //判断是否需要反显
                {
                    this.LoadSelectDept();   
                }
            }
        }

        /// <summary>
        /// 班级选择
        /// </summary>
        private void TV_LoadBJ()
        {
            this.TV_List.Nodes.Clear();
            List<xy_bmxbm> list = xy_bmxbm.All().ToList();
            List<xy_bj> bjlist = xy_bj.All().ToList();

            List<DataAssociate> associatelist = DataAssociate.Where("Typeid=@0", Convert.ToInt32(EnumData.AssociateType.系别与班级)).ToList();
            list.Where(p => p.BMFQID == "03").ToList().ForEach(j =>
            {
                TreeNode xynode = new TreeNode();
                xynode.Text = j.BMMC;
                xynode.Value = j.BMDM;
                xynode.Target = Convert.ToInt32(EnumData.TvNodeType.学院).ToString();

                list.Where(p => p.BMFQID == j.BMDM && p.BMMC.Contains("系")).ToList().ForEach(p =>
                {
                    TreeNode node = new TreeNode();
                    node.Text = p.BMMC;
                    node.Value = p.BMDM;
                    node.Target = Convert.ToInt32(EnumData.TvNodeType.系).ToString();

                    var s = from B in associatelist where B.ParentNo == p.BMDM select B.ChildNo;
                    var q = from A in bjlist
                            where s.Contains(A.BJDM)
                            select A;
                    List<xy_bj> childlist = q.ToList();
                    childlist.ForEach(o =>
                    {
                        TreeNode childNode = new TreeNode();
                        childNode.Text = o.BJMC;
                        childNode.Value = o.BJDM;
                        childNode.Target = Convert.ToInt32(EnumData.TvNodeType.班级).ToString();
                        childNode.ShowCheckBox = true;
                        node.ChildNodes.Add(childNode);
                    });
                    xynode.ChildNodes.Add(node);
                });
                TV_List.Nodes.Add(xynode);
            });
            this.TV_List.CollapseAll();
        }

        private void LoadSelectDept()
        {
            string strDept = "|" + Request.QueryString["DeptID"].ToString() + "|";
            List<TreeNode> list = this.TV_List.Nodes.AllNodes().ToList();
            list.ForEach(p =>
            {
                if (strDept.IndexOf("|" + p.Value + "|") > -1)
                {
                    p.Checked = true;
                }
            });
        }

        private void TVDataBind()
        {
            this.TV_List.Nodes.Clear();
            List<xy_bmxbm> list = xy_bmxbm.Where("BMFQID=@0", "03").ToList();
            list.ForEach(p=>{
                TreeNode node = new TreeNode();
                node.Value = p.BMDM;
                node.Text = p.BMMC;
                node.Target = p.BMDM+"|"+p.BMMC;
                if (Request.QueryString["IsSelectDept"].ToString() == "1")//判断是否选择部门
                {
                    this.AddChildNodes(node, p.BMDM);
                }
                else //选择部门的时候 不能选择学院
                { 
                    node.ShowCheckBox = true;
                }
                this.TV_List.Nodes.Add(node);
            });
            this.TV_List.CollapseAll();
        }

        private void AddChildNodes(TreeNode ParentNode, string BMDM)
        {
            List<xy_bmxbm> list = xy_bmxbm.Where("BMFQID=@0",BMDM).ToList();
            list.ForEach(p => {
                TreeNode node = new TreeNode();
                node.Value = p.BMDM;
                node.Text = p.BMMC;
                node.ShowCheckBox = true;
                //this.AddChildNodes(node, p.BMDM);  只加载一级部门
                ParentNode.ChildNodes.Add(node);
            });
        }

        /// <summary>
        /// 选择部门
        /// </summary>
        protected void btnSelectDept_Click(object sender, EventArgs e)
        {
            if (this.TV_List.CheckedNodes.Count <= 0)
            {
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "Alert", "alert('请选择部门!');", true);
                return;
            }

            if (this.HidIsRadio.Value == "1"&&this.TV_List.CheckedNodes.Count>1)//单选
            {
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "Alert", "alert('请选择单个部门!');", true);
                return;
            }
            
            StringBuilder sb_value = new StringBuilder();
            StringBuilder sb_text=new StringBuilder();
            foreach (TreeNode node in this.TV_List.CheckedNodes)
            {
                sb_value.Append(node.Value+"|");
                sb_text.Append(node.Text+"|");
            }

            if (sb_value.ToString().Length > 0)
            {
                ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", "SelectDeptMent('" + Request.QueryString["IsMdalDialog"].ToString() + "','" + sb_value.ToString().Trim('|') + "','" + sb_text.ToString().Trim('|') + "')", true);     
            }
        }
    }
}